var app = getApp()
var cartUtil = require("../../utils/cartUtil.js")
var util=require("../../utils/util.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    isAllSelect: false,
    sumPrice: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      list: cartUtil.getGoods()
    })
  },

  //待购买商品选择
  bindSelect(e) {
    var index = e.currentTarget.id
    cartUtil.updateSelect(index)
    this.setData({
      sumPrice: cartUtil.getSumPrice(),
      list: cartUtil.getGoods()
    })
  },
  //减少数量
  bindMinus(e) {
    var index = e.currentTarget.id
    cartUtil.updateNum(index, -1)
    this.setData({
      sumPrice: cartUtil.getSumPrice(),
      list: cartUtil.getGoods()
    })
  },
  //增加数量
  bindPlus(e) {
    var index = e.currentTarget.id
    cartUtil.updateNum(index, 1)
    this.setData({
      sumPrice: cartUtil.getSumPrice(),
      list: cartUtil.getGoods()
    })
  },
  //全选
  bindAllSelect() {
    cartUtil.allSelect(true)
    this.setData({
      sumPrice: cartUtil.getSumPrice(),
      isAllSelect: true,
      list: cartUtil.getGoods()
    })
  },
  //取消全选
  bindCancelAllSelect() {
    cartUtil.allSelect(false)
    this.setData({
      sumPrice: cartUtil.getSumPrice(),
      isAllSelect: false,
      list: cartUtil.getGoods()
    })
  },
  //结算
  navToOrderPay(){
    var list = cartUtil.getSelectGoods()
    if(!list||list.length==0){
      util.failToast('请选择商品')
      return
    }
    app.data.selectGoods=list
      wx.navigateTo({
        url: '../order_pay/order_pay',
      })
  }
})